//codes.js
system = require('system')
address = system.args[1];//获得命令行第二个参数 接下来会用到
//console.log('Loading a web page');
var page = require('webpage').create();
var url = address;

var loadInProgress = false, testindex = 0;

page.onLoadStarted = function () {
    loadInProgress = true;
};

page.onLoadFinished = function () {
    loadInProgress = false;
};

//构造一个步骤数组
var steps = [
    //第一步，打开需要登陆的网页
    function () {
        page.open(url);
        console.log(url);
    },
    //第二步，对于登陆的网页的表单进行填写和提交
    function () {
        page.evaluate(function (obj) {
            var loginButton = document.getElementsByClassName("")[0];
            loginButton.getElementsByTagName("span")[0].click();
            $("div.SignContainer-switch")
            document.forms[0].username.value = "15896637592";
            document.forms[0].password.value = "whl19931104";
            document.forms[0].submit();
            return document.title;
        });
        loadInProgress = true;
    },
    //第三步，获取登陆之后跳转的网页截图，并获取cookie值
    function () {
        page.render('登陆成功.png');//获取登陆成功够的屏幕截图
        var cookiesStr = "";
        var cookies = page.cookies;
        for (var i in cookies) {
            cookiesStr = cookiesStr + cookies[i].name + "=" + cookies[i].value + ";";
        }
        console.log(cookiesStr);
    }
];

//使用循环，获取数组中的内容
var interval = setInterval(function () {

    if (!loadInProgress && typeof steps[testindex] == "function") {
        steps[testindex]();
        testindex++;
    }
    if (typeof steps[testindex] != "function") {
        phantom.exit();
    }
}, 10);
